package com.xlf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xlf.Enum.StatusCodeEnum;
import com.xlf.constant.MessageConstant;
import com.xlf.domain.User;
import com.xlf.exception.BizException;
import com.xlf.mapper.MenuMapper;
import com.xlf.mapper.UserMapper;
import com.xlf.pojo.LoginUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 用户详情服务实现类
 */

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询用户信息，处理权限信息并校验用户状态
     * @param username  用户名
     * @return          用户详情信息
     * @throws UsernameNotFoundException    可能抛出的异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,username);
        User user = userMapper.selectOne(wrapper);
        //如果查询不到数据就通过抛出异常来给出提示
        if(Objects.isNull(user)){
            throw new RuntimeException(StatusCodeEnum.USERNAME_NOT_EXIST.getDesc());
        }
        if(user.getStatus().equals(1))
            throw new BizException(MessageConstant.USER_IS_DISABLE);
        List<String> perms = menuMapper.selectPermsByUserId(user.getId());
        System.out.println(perms);
        //封装成UserDetails对象返回
        return new LoginUser(user,perms);
    }
}
